home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4273 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.9 KB  |  75 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: johnb@pivotal-dm.ccmail.compuserve.com (John Bain)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: implicit converstion - error?
  5. Date: Mon, 29 Jan 1996 13:22:02 GMT
  6. Organization: Pivotal Technologies
  7. Message-ID: <310cc2a1.11393601@news.compuserve.com>
  8. References: <gibsonDLr0rs.AEB@netcom.com>
  9. NNTP-Posting-Host: hd07-006.compuserve.com
  10. X-Newsreader: Forte Agent .99c/16.141
  11.  
  12. gibson@netcom.com (Bob Gibson) wrote:
  13.  
  14. >  A friend showed me a compile error that I was able to recreate
  15. >with the following simple example:
  16. >
  17. >--
  18. >void test( const char * * ppc )
  19. >{
  20. >  cout << '"' << *ppc << '"' << endl;
  21. >}
  22. >
  23. >int main()
  24. >{
  25. >   char * msg = "Example message";
  26. >   char * * p = &msg;
  27. >
  28. >   test( ppc );            // <- Error
  29. >   test( &msg );           // <- Error
  30. >
  31. >}
  32. >--
  33. >  OK.  Here's the problem.  The compiler complains that a
  34. >"char * *" can't / won't be converted to a "const char * *".
  35. >I don't understand the proglem.  If the function signature
  36. >says that it will treat the "char * *" in such a was as to
  37. >not change the chars' to which the pointer point, what's
  38. >the problem?
  39. >
  40.  
  41. Conversion from char** -> const char ** is not 
  42. allowed, because it is potentially unsafe.  Consider
  43. the following code:
  44.  
  45. --------------------------------------------------
  46.  
  47. const char c_ch;
  48.  
  49. const char **pp_c_ch;
  50. char *p_ch;
  51.  
  52. pp_c_ch = &p_ch;  /* OK _if_ char** -> const char ** allowed */
  53. *pp_c_ch = &c_ch; /* allowed - assigning &<const char> to const char* 
  54.                   */
  55.                   /* p_ch now points at c_ch */
  56. *p_ch = 'X';      /* OOPS, modified c_ch! */
  57.  
  58. ---------------------------------------------------
  59.  
  60.  
  61. >Thanks in advance
  62. >
  63. >Bob
  64. >-- 
  65. >O.J. Verdict - The best innocence money can buy
  66. >
  67. >Bob Gibson -- gibson@netcom.com
  68.  
  69. John
  70. -----------------------------------------------------------------
  71. John Bain                  
  72. johnb@pivotal-dm.ccmail.compuserve.com 
  73. -----------------------------------------------------------------
  74.  
  75.